package top.ffhome.concurrency.example.singleton;

import top.ffhome.common.annotation.ThreadSafe;

/**
 * 懒汉模式 -- 双重同步锁单例模式,volatile防止指令重排
 */
@ThreadSafe
public class SingletonExample5 {
    private SingletonExample5() {
    }

    private static volatile SingletonExample5 instance = null;

    public static SingletonExample5 getInstance() {
        if (instance == null) {
            synchronized (SingletonExample5.class) {
                if (instance == null) {
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }
}
